package cn.com.yusys.yusp.admin.service.impl;

import cn.com.yusys.yusp.admin.domain.AdminSmProp;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmPropMapper;
import cn.com.yusys.yusp.admin.service.PropProviderService;
import cn.com.yusys.yusp.commons.dto.def.ContrBean;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class PropProviderServiceImpl implements PropProviderService {

	@Autowired
	private AdminSmPropMapper adminSmPropMapper;

	public String pathRestore(String path) throws JsonProcessingException {
		if (path.contains("-AGRE-")) {
//			QueryModel model = new QueryModel();
//			Map<String, String> map = new HashMap<String, String>();
//			map.put("propName", "-AGRE-");
//			ObjectMapper jsonObj = new ObjectMapper();
//			model.setCondition(jsonObj.writeValueAsString(map));
//			String agreConfig = this.adminSmPropMapper.getPropListBycodeOrName(model).get(0).get("propValue");
			String agreConfig ="";
			List<AdminSmProp> propList = findAllProp();
			if (propList != null && !propList.isEmpty()) {
				List<AdminSmProp>data=propList.stream().filter(prop -> prop.getPropName().equals("-AGRE-")).collect(Collectors.toList());
				if (data != null && !data.isEmpty()) {
					agreConfig = data.get(0).getPropValue();
				}
			}
			path = path.replace("-AGRE-", agreConfig);
		}
		if (path.contains("-HOST-")) {
//			QueryModel model = new QueryModel();
//			Map<String, String> map = new HashMap<String, String>();
//			map.put("propName", "-HOST-");
//			ObjectMapper jsonObj = new ObjectMapper();
//			model.setCondition(jsonObj.writeValueAsString(map));
//			String hostConfig = this.adminSmPropMapper.getPropListBycodeOrName(model).get(0).get("propValue");
			String hostConfig ="";
			List<AdminSmProp> propList = findAllProp();
			if (propList != null && !propList.isEmpty()) {
				List<AdminSmProp>data=propList.stream().filter(prop -> prop.getPropName().equals("-HOST-")).collect(Collectors.toList());
				if (data != null && !data.isEmpty()) {
					hostConfig = data.get(0).getPropValue();
				}
			}
			path = path.replace("-HOST-", hostConfig);
			// path = path.replace("-HOST-", "//192.168.251.151");
		}
		if (path.contains("-PORT-")) {
//			QueryModel model = new QueryModel();
//			Map<String, String> map = new HashMap<String, String>();
//			map.put("propName", "-PORT-");
//			ObjectMapper jsonObj = new ObjectMapper();
//			model.setCondition(jsonObj.writeValueAsString(map));
//			String portConfig = this.adminSmPropMapper.getPropListBycodeOrName(model).get(0).get("propValue");
			String portConfig ="";
			List<AdminSmProp> propList = findAllProp();
			if (propList != null && !propList.isEmpty()) {
				List<AdminSmProp>data=propList.stream().filter(prop -> prop.getPropName().equals("-PORT-")).collect(Collectors.toList());
				if (data != null && !data.isEmpty()) {
					portConfig = data.get(0).getPropValue();
				}
			}
			path = path.replace("-PORT-", portConfig);
		}
		return path;
	}

	/**
	 * 根据属性名获得属性值
	 * @param propName 属性名
	 * @return
	 */
	public String getPropValueByPropName(String propName) {
//		QueryModel queryModel = new QueryModel();
		String propValue = "";
//		queryModel.getCondition().put("propName", propName);
//		List<AdminSmProp> propList = this.adminSmPropMapper.selectByModel(queryModel);
//		if (propList != null && !propList.isEmpty()) {
//			propValue = propList.get(0).getPropValue();
//		}
		List<AdminSmProp> propList = findAllProp();
		if (propList != null && !propList.isEmpty()) {
			List<AdminSmProp>data=propList.stream().filter(prop -> prop.getPropName().equals(propName)).collect(Collectors.toList());
			if (data != null && !data.isEmpty()) {
				propValue = data.get(0).getPropValue();
			}
		}
		return propValue;
	}
	/**
	 * 
	 * @方法名称:findAllProp
	 * @方法描述:查询所有的参数信息
	 * @参数与返回说明:
	 * @算法描述:如果缓存中有就从缓存取，缓存没有就查询数据库取，并把查询出来的结果放入缓存
	 */
    @Transactional(readOnly = true)
    @Cacheable(value = "propInfo", key = "'findAllProp'", unless = "#result == null")
    public List<AdminSmProp> findAllProp() {
    	QueryModel model = new QueryModel();
        return this.adminSmPropMapper.selectByModel(model);
    }
    /**
	 * 
	 * @方法名称:cleanAllProp
	 * @方法描述:清除缓存
	 * @参数与返回说明
	 */
    @CacheEvict(value = "propInfo", key = "'findAllProp'", beforeInvocation=true)
    public void cleanAllProp() {

    }
}